﻿using System.Collections.Generic;
using System.Linq;

namespace Medianamik.Core.Synchronization
{
    public class NodeToRecordConverter : GenericNodeToRecordConverter<Node, NodeProperty>
    {
        public override Record GetRecord(Node node)
        {
            return new Record
                {
                    Id = node.ID,
                    Name = node.Name,
                    NodeTypeId = node.NodeTypeID,
                    ParentId = node.ParentID,
                    Position = node.Position
                };
        }

        public override IEnumerable<NodeProperty> GetProperties(Node node)
        {
            return node.Instances.SelectMany(i => i.Properties).Distinct();
        }

        public override RecordProperty GetRecordProperty(NodeProperty property)
        {
            return new RecordProperty
                {
                    NodeTypePropertyId = property.NodeTypePropertyID,
                    Value = property.Value,
                    CultureName = property.IsLookup ? "" : property.Culture.Name,
                    NodeTypePropertyName = property.NodeTypeProperty.Name,
                    PropertyType = property.IsOwnProperty ? 
                        PropertyType.Default : PropertyType.ManyToMany
                };
        }
    }
}